﻿#include <conio.h>
#include <graphics.h>
#pragma comment(lib,"Winmm.lib")
#define width 500
#define high 700 

IMAGE img_bk;		//背景图片
IMAGE img_planenomal1, img_planenomal2;//正常飞机
IMAGE img_bullet1, img_bullet2;			//子弹位置
IMAGE img_enemyplane1, img_enemyplane2;		//敌机
IMAGE img_siji1,img_siji2;					//死亡飞机
float enemy_x, enemy_y;			//敌机位置
int bulllet_x, bullet_y;		//子弹位置
int position_x, position_y;		//飞机坐标

void startup() // 数据初始化
{
	mciSendString(_T("open D:\\feiji\\game_music.mp3 alias bkmusic"), NULL, 0, NULL);//背景音乐
	mciSendString(_T("play bkmusic repeat"), NULL, 0, NULL);
	initgraph(width, high);
	position_x = width / 2;
	position_y = high / 2;
	bulllet_x = position_x;
	bullet_y = -100;
	enemy_x = width / 2;
	enemy_y = 0;
	loadimage(&img_bk, _T("D:\\feiji\\background.jpg"));
	loadimage(&img_planenomal1, _T("D:\\feiji\\planeNormal_1.jpg"));
	loadimage(&img_planenomal2, _T("D:\\feiji\\planeNormal_2.jpg"));
	loadimage(&img_bullet1, _T("D:\\feiji\\bullet1.jpg"));
	loadimage(&img_bullet2, _T("D:\\feiji\\bullet2.jpg"));
	loadimage(&img_enemyplane1, _T("D:\\feiji\\enemyPlane1.jpg"));
	loadimage(&img_enemyplane2, _T("D:\\feiji\\enemyPlane2.jpg"));
	loadimage(&img_siji1, _T("D:\\feiji\\planeExplode_1.jpg"));
	loadimage(&img_siji2, _T("D:\\feiji\\planeExplode_2.jpg"));
	BeginBatchDraw();
}

void show()  // 显示画面
{
	putimage(0, 0, &img_bk);		//显示背景
	putimage(position_x-50, position_y-60, &img_planenomal1, NOTSRCERASE);		//显示正常飞机
	putimage(position_x-50, position_y-60, &img_planenomal2, SRCINVERT);
	putimage(bulllet_x, bullet_y, &img_bullet1, NOTSRCERASE);		//显示子弹
	putimage(bulllet_x, bullet_y, &img_bullet2, SRCINVERT);
	putimage(enemy_x, enemy_y, &img_enemyplane1, NOTSRCERASE);		//显示敌机
	putimage(enemy_x, enemy_y, &img_enemyplane2, SRCINVERT);



	FlushBatchDraw();

	Sleep(5);

}

void updateWithoutInput()  // 与用户输入无关的更新
{
	if(bullet_y>-50)
		bullet_y = bullet_y - 2;			//子弹向上移动
	if (enemy_y < high)
		enemy_y=enemy_y+0.5;
	else {
		enemy_x = rand() % width;
		if(enemy_x>350)
			enemy_x = rand() % width;
		enemy_y = -148;
	}
	if (bulllet_x >= enemy_x && bulllet_x <= enemy_x + 104)//子弹击中敌机
	{
		if (bullet_y <= enemy_y)
		{
			enemy_x = rand() % width;
			if (enemy_x > 350)
			enemy_x = rand() % width;
				enemy_y = -148;
				mciSendString(_T("close music"), NULL, 0, NULL);//关前一次音乐
				mciSendString(_T("open D:\\feiji\\gotEnemy.mp3 alias music"), NULL, 0, NULL);
				mciSendString(_T("play music"), NULL, 0, NULL);//仅播一次
		}
	}
	if (position_x >= enemy_x + 100 || position_x + 100 <= enemy_x)
	{
		if (position_y <= enemy_y)
		{
			putimage(position_x - 50, position_y - 60, &img_siji1, NOTSRCERASE);		//显示死机
			putimage(position_x - 50, position_y - 60, &img_siji2, SRCINVERT);
			_getch();
		}
	}






}

void updateWithInput()  // 与用户输入有关的更新
{
	MOUSEMSG m;		//记录鼠标消息
	while (MouseHit())
	{
		m = GetMouseMsg();
		if (m.uMsg == WM_MOUSEMOVE)//	飞机位置等于鼠标位置
		{
			position_x = m.x;
			position_y = m.y;
		}
		else if (m.uMsg == WM_LBUTTONDOWN)//按下鼠标左键发射子弹
		{
			bulllet_x = position_x-5;
			bullet_y = position_y - 85;
			mciSendString(_T("close jpmusic"), NULL, 0, NULL);//关前一次音乐
			mciSendString(_T("open D:\\feiji\\f_gun.mp3 alias jpmusic"), NULL, 0, NULL);
			mciSendString(_T("play jpmusic"), NULL, 0, NULL);//仅播一次
		}








	}


}
void gameover()
{
	EndBatchDraw();
	_getch();
	closegraph();
}

int main()
{
	startup();  // 数据初始化
	while (1) //  游戏循环执行
	{

		show();  // 显示画面
		updateWithoutInput();  // 与用户输入无关的更新
		updateWithInput();  // 与用户输入有关的更新
	}
	_getch();
	gameover();
	return 0;

}